package de.reminder.client.widgets;

import com.google.gwt.animation.client.Animation;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style;
import com.google.gwt.user.client.ui.RootPanel;

public class MoveAnimation extends Animation {
	private final ReminderView element;
	private int startX,startY,finalX,finalY;
	
	public MoveAnimation(ReminderView element){
		this.element = element;
	}
	
	public void moveTo(int x,int y, int milliseconds){
		this.finalX = x;
		this.finalY = y;
		
		startX = element.getElement().getOffsetLeft();
		startY = element.getElement().getOffsetTop();
		
		run(milliseconds);
	}
	
	@Override
	protected void onUpdate(double progress) {
		double positionX = startX + (progress * (this.finalX - startX));
        double positionY = startY + (progress * (this.finalY - startY));
        
        this.element.getElement().getStyle().setLeft(positionX, Style.Unit.PX);
        this.element.getElement().getStyle().setTop(positionY, Style.Unit.PX);
	}
	
	@Override
    protected void onComplete(){
        super.onComplete();
        this.element.getElement().getStyle().setLeft(this.finalX, Style.Unit.PX);
        this.element.getElement().getStyle().setTop(this.finalY, Style.Unit.PX);
    }

}
